package org.jsola.hr.helper;

import org.jsola.common.DateKit;
import org.jsola.hr.dto.EmpPositionAddDTO;
import org.jsola.hr.dto.EmpPositionUpdateDTO;
import org.jsola.hr.entity.EmpPositionDO;
import org.jsola.hr.query.EmpPositionQuery;
import org.jsola.hr.service.IEmpPositionService;
import org.jsola.hr.vo.EmpPositionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class EmpPositionTestHelper extends BaseTestHelper {
    
    @Autowired
    private EmpInfoTestHelper empInfoTestHelper;
    @Autowired
    private IEmpPositionService empPositionService;
    @Autowired
    private PositionInfoTestHelper positionInfoTestHelper;

    /**
     * 生成add测试数据
     * @return EmpPositionAddDTO
     */
    public EmpPositionAddDTO getEmpPositionAddDTO(){
        EmpPositionAddDTO empPositionAddDTO = new EmpPositionAddDTO();
        empPositionAddDTO.setEmpId(empInfoTestHelper.saveEmpInfoExample().getId());
        empPositionAddDTO.setPositionId(positionInfoTestHelper.savePositionInfoExample().getId());
        empPositionAddDTO.setCompanyId(getRandomValue("varchar",32));
        return empPositionAddDTO;
    }


    /**
     * 生成update测试数据
     * @return EmpPositionUpdateDTO
     */
    public EmpPositionUpdateDTO getEmpPositionUpdateDTO(EmpPositionVO empPositionVO){
        return empPositionService.selectDOById(empPositionVO.getId(), SITE_ID).to(EmpPositionUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return EmpPositionQuery
     */
    public EmpPositionQuery getEmpPositionQuery() {
        EmpPositionQuery empPositionQuery = new EmpPositionQuery();
        List<EmpPositionDO> empPositionDOList = empPositionService.list(new EmpPositionDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(empPositionDOList)) {
            EmpPositionDO empPositionDO = empPositionDOList.get(0);
            empPositionQuery = empPositionDO.to(EmpPositionQuery.class);
            if (empPositionDO.getGmtCreate() != null) {
                empPositionQuery.setGmtCreateStart(DateKit.getBeforeDay(empPositionDO.getGmtCreate(),-1));
                empPositionQuery.setGmtCreateEnd(DateKit.getBeforeDay(empPositionDO.getGmtCreate(),1));
            }
        }
        return empPositionQuery;
    }

    /**
     * 保存示例
     * @return EmpPositionVO
     */
    public EmpPositionVO saveEmpPositionExample(){
        EmpPositionAddDTO addDTO = this.getEmpPositionAddDTO();
        EmpPositionVO empPositionVO = empPositionService.save(addDTO,buildTokenUser());
        return empPositionVO;
    }


}

